package com.psjj.privilege.web.servlet;

import com.psjj.privilege.po.Role;
import com.psjj.privilege.service.RoleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/role.do")
public class RoleController extends BasicServlet{
    private RoleService roleService=new RoleService();

    private static final String BASIC_URL="/WEB-INF/page/role/";
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Role> list = roleService.queryRoles();
        req.setAttribute("list",list);
        req.getRequestDispatcher(BASIC_URL+"table.jsp").forward(req,resp);


    }
    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(BASIC_URL+"add.jsp").forward(req,resp);
       
    }
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rname = req.getParameter("rname");
        String description = req.getParameter("description");
        Role role=new Role();
        role.setRname(rname);
        role.setDescription(description);
        roleService.add(role);
        resp.sendRedirect(req.getContextPath()+"/role.do?method=list");

    }
    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int rid=Integer.parseInt(req.getParameter("rid"));
        Role role = roleService.queryRoleById(rid);
        req.setAttribute("role",role);
        req.getRequestDispatcher(BASIC_URL+"update.jsp").forward(req,resp);

       
    }
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int rid=Integer.parseInt(req.getParameter("rid"));
        String rname = req.getParameter("rname");
        String description = req.getParameter("description").trim();
        Role role = new Role();
        role.setRid(rid);
        role.setRname(rname);
        role.setDescription(description);
        roleService.editRole(role);
        resp.sendRedirect(req.getContextPath()+"/role.do?method=list");




    }
    private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int rid = Integer.parseInt(req.getParameter("rid"));
        roleService.del(rid);
        resp.sendRedirect(req.getContextPath()+"/role.do?method=list");

    }

}
